package com.chixing.controller;

import com.chixing.commons.Result;
import com.chixing.commons.TokenUtil;
import com.chixing.pojo.LoginPerson;
import com.chixing.pojo.Myorders;
import com.chixing.pojo.ResumeTemplate;
import com.chixing.service.IMyordersService;
import com.chixing.service.IResumeTemplateService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDate;
import java.util.UUID;

/**
 * <p>
 * 用户购买简历记录（订单） 前端控制器
 * </p>
 *
 * @author Jason Marslyon
 * @since 2022-10-04
 */
@Controller
@RequestMapping("myOrders")
public class MyordersController {
    @Autowired
    private IMyordersService myOrdersService;

    @Autowired
    private IResumeTemplateService resumeTemplateService;

    @Autowired
    private TemplateKillController templateKillController;

    @Autowired
    private AmqpTemplate template;

    @GetMapping ("/{templateId}/{token}")
    public ModelAndView initMyOrder(@PathVariable("templateId") Integer templateId,
                                    @PathVariable("token") String token){
        //普通模板
        ResumeTemplate resumeTemplate = resumeTemplateService.selectById(templateId);
        Myorders myOrder = new Myorders();
        myOrder.setTemplateId(templateId);
        LoginPerson loginPerson = TokenUtil.parseToken(token);
        myOrder.setPersonId(loginPerson.getPersonId());
        myOrder.setOrderCreateTime(LocalDate.now());
        myOrder.setOrderPrice(resumeTemplate.getTemplateSalary());
        UUID uuid = UUID.randomUUID();
        String uuid1 = uuid.toString().replace("-", "");
        System.out.println(uuid1);
        myOrder.setOrderNumber(uuid1);
        myOrder.setOrderState(0);//订单状态0未支付，1已支付
        Boolean aBoolean = myOrdersService.insertMyOrder(myOrder);
        Myorders myorders = myOrdersService.selectByOrderNumber(uuid1);
        ModelAndView modelAndView = new ModelAndView();
        if (aBoolean) {
            modelAndView.addObject("myOrdersList", myorders);
            modelAndView.addObject("template", resumeTemplate);
            modelAndView.setViewName("public/resume/resume-orders");
            template.convertAndSend("orderDelayExchange", "orderDelayKey", uuid1);
        } else {
            modelAndView.addObject("myOrdersList", "生成订单失败");
            modelAndView.setViewName("public/resume/resume-orders");
        }
        return modelAndView;
    }

    @GetMapping ("/updateOrder")
    public Result updateOrder(Integer orderId){
        Myorders myorders = myOrdersService.selectByOrderId(orderId);
        //不管是否支付，设置未已支付
        myorders.setOrderState(1);
        boolean b = myOrdersService.updateMyOrder(myorders);
        if(b) return new Result(200,"成功",b);
        return new Result(201,"失败",b);

    }

    @GetMapping ("/selectById")
    public Result selectById(Integer orderId){
        Myorders myorders = myOrdersService.selectByOrderId(orderId);
        if(myorders!=null) return new Result(200,"成功",myorders);
        return new Result(201,"失败",null);
    }
}

